package com.ybf.api.mapper;

import com.ybf.api.entity.User;
import com.ybf.api.mapper.provider.UserProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UsersMapper {

    @SelectProvider(type = UserProvider.class, method = "findAll")
    List<User> list(@Param("user") User user);

    @Delete("delete from users where id = #{id}")
    Integer delete(@Param("id") Integer id);

    @Select("select username, phone, img, sex, intro, status, updated_at, created_at, id, nickname from users where id = #{id}")
    User info(@Param("id") Integer id);


    @Insert("insert into users (username, phone, img, sex, intro, status, nickname, password) values (#{user.username}, #{user.phone}, #{user.img}, #{user.sex}, #{user.intro}, #{user.status}, #{user.nickname}, #{user.password})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    Integer add(@Param("user") User user);

    @Update("update users set username = '${user.username}', phone = '${user.phone}', img = '${user.img}', sex = '${user.sex}', intro = '${user.intro}', status = '${user.status}', nickname = '${user.nickname}', password = '${user.password}' where id = ${user.id}")
    Integer edit(@Param("user") User user);

    @Select("select id, username, phone, img, sex, intro, status, updated_at, created_at, nickname from users where username = #{username} and password = #{password} and status = 1")
    User findByUsernameAndPassword(@Param("username") String username, @Param("password") String password);
}
